import Vue from 'vue'

// import _gobal from '../../base/Gobal'
// import { Popup } from 'vux'
// import { Toast } from 'vue-ydui/dist/lib.px/dialog'
import { Button } from 'vue-ydui/dist/lib.px/button'

Vue.component(Button.name, Button)

export default {
    data: function () {
        return {
            show:true,
            // sourcePic: _gobal.sourcePic,
            showPayMethods: false,
            // activeName: '微信支付',
            orderId: '',
            orderDatas: [],
            orderInfo: {},
            total: '',
            orderAddress: '',
            orderPhone: '',
            orderLinker: '',
            wechatInfo: '',
            // hasAddressFlag: false,
            level: '',
            discountPrice: '',
            discountFlag: false,
            balance: '',
            goodid: '',
            coun: '',
            order: '',
            gids: [],
            temp: '',
            from: '',
            orderAddressid: ''
        }
    },
    components: {
        // Popup
    },
    mounted: function () {
        this.getBalance()
        var _this = this;
        if ((this.$route.query.from == 'details') || (this.$route.query.from == 'buyOrder')) {
            // 来自detail.vue
            // this.orderId = this.$route.query.ordersn;
            this.from = 'details'
            if (this.$route.query.id) {
                var addid = this.$route.query.id;
                _this.getAddress(addid)
            } else {
                _this.getAddress()
            }
            this.goodid = this.$route.query.gid;
            this.coun = this.$route.query.amount;
            this.axios.get('/order/preview_goods', {
                params: {
                    openid: sessionStorage.getItem('openid'),
                    // openid: 'oajse0aHO-bQEh0OHbIJIHuNDAJE',
                    gid: this.goodid,
                    amount: this.coun
                }
            }).then(function (res) {
                _this.orderDatas = res.data.datas.goods;
                _this.order = res.data.datas.order;
                // _this.orderInfo = res.data.datas.orderinfo;
            }, function (err) {
                console.log(err);
            })
            // if(this.$route.query.id){
            //     _this.getAddress(addid)
            // }else{
            //     _this.getAddress()

            // }
        } else if (this.$route.query.from == 'selectAddress') {
            // 来自selectAddress.vue
            // this.axios.post('/order/info?ordersn=' + this.$route.query.orderId).then(function (res) {
            let addid = this.$route.query.id;
            this.goodid = this.$route.query.gid;
            this.coun = this.$route.query.amount;
            //     this.axios.post('/order/info').then(function (res) {
            //     _this.orderDatas = res.data.datas.ordergoodsinfo;
            //     // _this.orderInfo = res.data.datas.orderinfo;
            // }, function (err) {
            //     console.log(err);
            // })


            // this.axios.get('/order/preview_goods', {
            //     params: {
            //         openid: sessionStorage.getItem('openid'),
            //         // openid: 'oajse0aHO-bQEh0OHbIJIHuNDAJE',
            //         gid: this.goodid,
            //         amount: this.coun
            //     }
            // }).then(function (res) {
            //     _this.orderDatas = res.data.datas.goods;
            //     _this.order = res.data.datas.order;
            // }, function (err) {
            //     console.log(err);
            // })
            _this.getAddress(addid)
        } else if (this.$route.query.from == 'shop') {
            let addid = this.$route.query.id;
            this.from = 'shop'
            this.temp = this.$route.query.temp;
            console.log('TEMP')
            console.log(typeof this.temp)
            console.log(this.temp)
            this.axios.get('/order/preview_trolley', {
                params: {
                    openid: sessionStorage.getItem('openid'),
                    // openid: 'oajse0aHO-bQEh0OHbIJIHuNDAJE',
                    orderinfo: this.temp,
                }
            }).then(function (res) {
                _this.orderDatas = res.data.datas.goods;
                _this.order = res.data.datas.order;
            }, function (err) {
                console.log(err);
            })
            if (addid) {
                _this.getAddress(addid)
            } else {
                _this.getAddress()
            }

            // console.log("goodid", this.gids);
        }
        // _this.getAddress()
        // _this.getLevel()
    },
    filters: {
        formatMoney: function (value) {
            if (typeof value == "number") {
                return "￥" + value.toFixed(2);
            }
        }
    },
    methods: {
        goToDetails: function (id) {
            // this.$router.push("/details?id=" + id);
        },
        // gotoSelectAddress: function (from) {
        //     // this.$router.replace('/selectAddress?from=settlement&orderId=' + this.orderInfo.ordersn);
        //     if (from == 'shop') {
        //         this.$router.replace('/selectAddress?from=shop&temp=' + this.temp);
        //     }
        //     if (from == 'details') {
        //         this.$router.replace('/selectAddress?from=details&gid=' + this.goodid + '&amount=' + this.coun);
        //     }
        // },
        // 更换支付方式
        changePaidMethod: function (num, payNames) {
            this.payName = payNames
            // this.activeName = this.payName
            this.showPayMethods = false;
        },
        // 获取会员等级
        // getLevel: function () {
        //     this.axios.post("/member/info", { "openid": sessionStorage.getItem('openid') }).then(res => {
        //         this.level = res.data.member_level
        //         if (this.level == 0) {
        //             this.discountFlag = false
        //         } else {
        //             this.discountFlag = true
        //             if (this.level == 1) {
        //                 this.discountPrice = parseFloat(this.totalPrice * 0.75)
        //             } else if (this.level == 2) {
        //                 this.discountPrice = parseFloat(this.totalPrice * 0.65)
        //             }
        //         }
        //     }, function (err) {
        //         console.log(err.data);
        //     })
        // },
        // 获取地址列表
        getAddress: function (id) {
            var _this = this;
            // this.axios.get('/member/address/all', {
            //     params: {
            //         // "openid": 'oajse0aHO-bQEh0OHbIJIHuNDAJE'
            //         openid: sessionStorage.getItem('openid')
            //     }
            // }).then(function (res) {
            //     if (res.data.datas.length > 0) {
            //         if (id) {
            //             res.data.datas.forEach(function (value, index) {
            //                 if (value.id == id) {
            //                     _this.orderLinker = value.consignee;
            //                     _this.orderPhone = value.phone;
            //                     _this.orderAddress = value.province + value.city + value.district + value.detail;
            //                     _this.orderAddressid = value.id
            //                 }
            //             });
            //         } else {
            //             res.data.datas.forEach(function (value, index) {
            //                 if (value.is_default == 1) {
            //                     _this.orderLinker = value.consignee;
            //                     _this.orderPhone = value.phone;
            //                     _this.orderAddress = value.province + value.city + value.district + value.detail;
            //                     _this.orderAddressid = value.id
            //                 }
            //             });
            //         }
            //         // _this.hasAddressFlag = true
            //     } else {
            //         // _this.hasAddressFlag = false
            //     }
            // }, function (err) {
            //     console.log(err);
            // });
        },
        // 提交订单
        submit: function () {

            // 判断有没有地址，如果没有，前端提示
            // if (this.orderAddress == '' || this.orderAddress == '未设置') {
            //     Toast({
            //         mes: '收货地址不能为空',
            //         timeout: 1000
            //     });
            //     return
            // }
            // 判断折扣
            // if (this.level != 0) {
            //     this.total = this.discountPrice.toFixed(2)
            // }
            // 如果选择的是微信支付方式 才唤起
            if (this.activeName == '微信支付') {
                // this.axios.post('/order/pay', {
                //     // price: this.total,
                //     // ordersn: this.orderInfo.ordersn,
                //     openid: sessionStorage.getItem('openid'),
                //     // openid: 'oajse0aHO-bQEh0OHbIJIHuNDAJE',
                //     // linker: this.orderLinker,
                //     // mobile: this.orderPhone,
                //     address: this.orderAddressid,
                //     order: this.order,
                // }).then(res => {

                //     this.wechatInfo = res.data.datas
                //     console.log(this.wechatInfo)
                //     if (typeof WeixinJSBridge == "undefined") {
                //         if (document.addEventListener) {
                //             document.addEventListener('WeixinJSBridgeReady', this.jsApiCall, false);
                //         } else if (document.attachEvent) {
                //             document.attachEvent('WeixinJSBridgeReady', this.jsApiCall);
                //             document.attachEvent('onWeixinJSBridgeReady', this.jsApiCall);
                //         }
                //     } else {
                //         this.jsApiCall();
                //     }
                // }, function (err) {
                //     console.log(err);
                // })
            }
            // // 选择余额支付
            if (this.activeName == '余额支付') {
                if (parseFloat(this.total) > parseFloat(this.balance)) {
                    Toast({
                        mes: '您的余额不足，请选择其他方式支付'
                    });
                    return
                }
                // this.axios.post('/order/balance/buy/pay', {
                //     price: this.total,
                //     openid: sessionStorage.getItem('openid'),
                //     // ordersn: this.orderInfo.ordersn,
                //     linker: this.orderLinker,
                //     mobile: this.orderPhone,
                //     address: this.orderAddress
                // }).then(res => {
                //     if (res.data.result) {
                //         this.$router.push('/buyOrder')
                //     }
                //     Toast({
                //         mes: res.data.message
                //     })
                // }, function(err) {
                //     alert(err)
                //     console.log(err);
                // })
            }
            // // 银行卡支付
            // if (this.activeName == '银行卡支付') {
            //     this.total = parseFloat(this.total) * 100
            //     window.location.href = "http://sljspapitest.anasit.com/order/unionpay/buy/pay?price=" + this.total + "&openid=" + sessionStorage.getItem('openid') + "&ordersn=" + this.orderInfo.ordersn + "&linker=" + this.orderLinker + "&mobile=" + this.orderPhone + "&address=" + this.orderAddress
            // }

        },
        // 微信支付
        jsApiCall: function () {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', {
                    "appId": this.wechatInfo.datas.appId, //公众号名称，由商户传入     
                    "timeStamp": this.wechatInfo.datas.timeStamp, //时间戳，自1970年以来的秒数     
                    "nonceStr": this.wechatInfo.datas.nonceStr, //随机串     
                    "package": this.wechatInfo.datas.package,
                    "signType": this.wechatInfo.datas.signType, //微信签名方式：     
                    "paySign": this.wechatInfo.datas.paySign //微信签名 
                },
                function (res) {
                    WeixinJSBridge.log(res.err_msg);
                    //    alert(JSON.stringify(res));
                    //    alert("支付成功！asdf");
                    //    alert(res.err_code+res.err_desc+res.err_msg);
                    if (res.err_msg == "get_brand_wcpay_request:ok") { //如果微信支付成功
                        Toast({
                            mes: "支付成功",
                            timeout: 1000
                        });
                        // window.location.href = "http://dmhshop.anasit.com/buyOrder"; //成功后要跳转的页面或控制器
                    } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                        //如果取消微信支付
                        Toast({
                            mes: "您已取消支付",
                            timeout: 1000
                        });
                        // window.location.href = "http://dmhshop.anasit.com/buyOrder"; //成功后要跳转的页面或控制器
                    }
                }
            );
        },
        // 获取用户余额
        getBalance: function () {
            // this.axios.post("/member/info", {
            //     "openid": sessionStorage.getItem('openid')
            // }).then(res => {
            //     this.balance = res.data.balance
            // }, function (err) {
            //     console.log(err.data);
            // })
        }
    },
    computed: {
        totalPrice: function () {
            var num = 0;
            this.orderDatas.forEach(function (item, index) {
                num += item.price * parseInt(item.amount);
            });
            this.total = num
            return num;
        }
    }
}